Test Series - java script

Test Number 11/92

Q: What will be the output of the following JavaScript statement?

var grand_Total=eval("10*10+5");
A. 10*10+5
B. 105 as a string
C. 105 as an integer value
D. Exception is thrown
Solution: eval() is a function property of the global object. The argument of the eval() function is a string. If the string represents an expression, eval() evaluates the expression. If the argument represents one or more JavaScript statements, eval() evaluates the statements.
Q: Do functions in JavaScript necessarily return a value?
A. It is mandatory
B. Not necessary
C. Few functions return values by default
D. some functions do not return any value
Solution: Functions generally have a return statement and hence usually returns a value. Some functions which does not have a return statement returns value by default during execution.
Q: Will the following JavaScript code work?

var tensquared = (function(x) {return x*x;}(10));
A. Yes, perfectly
B. Error
C. Exception will be thrown
D. Memory leak
Solution: Function name is optional for functions defined as expressions. Function expressions are sometimes defined and immediately invoked.
Q: What will be the output of the following JavaScript code?

var string2Num=parseInt("123xyz");
A. 123
B. 123xyz
C. Exception
D. NaN
Solution: The parseInt() function parses a string and returns an integer. The function returns the first integer contained in the string or 0 if the string does not begin with an integer.
Q: If you have a function f and an object o, you can define a method named m of o with ________
A. o.m=m.f;
B. o.m=f;
C. o=f.m;
D. o=f;
Solution: A method is nothing more than a JavaScript function that is stored in a property of an object. If you have a function f and an object o, you can define a method named m of o with the following line:
o.m = f;
Q: What will be the output of the following JavaScript code?

var arr = [7, 5, 9, 1];  
var min = Math.min.apply(null, arr);  
document.writeln(min);
A. 7
B. 5
C. 1
D. 9
Solution: The function apply() is used to call a function that contains “this” value and the argument contains elements of an array. Unlike call() method, it contains a single array of arguments.
Q: What will be the output of the following JavaScript code?

var add=new Function("num1","num2","return num1+num2");  
document.writeln(add(2,5));
A. Error
B. 2
C. 5
D. 7
Solution: add function is defined in the first line of the code using the new property. Add function returns the sum of the two arguments passed to it.
Q: What will be the output of the following JavaScript code?

var a=3.7;
var b=2;
a=ciel(a)
document.writeIn(a*b);
A. 7.5
B. 6
C. 7
D. 8
Solution: The ciel function in javascript round offs the value passed in its argument to the next higher closest value. Therefore the value of a will be converted to 4 and hence the output will be 8.
Q: What will be the output of the following JavaScript code?

var a=2.99;
var ans=floor(a)*floor(a)
console.log(ans);
A. 9
B. 8.31
C. Error
D. 4
Solution: The floor method round offs the value of the decimal number to the lower limit. Therefore floor of a will be 2 which will result in the output of 4.
Q: What will be the output of the following JavaScript code?

var a=225;
document.writeln(Math.sqrt(a));
A. 225
B. 15
C. Error
D. Undefined
Solution: The JavaScript math sqrt() method returns the square root of a number. If the provided number is negative, it returns NaN.

You Have Score    /10